package com.jzo2o.customer.controller.consumer;

import com.jzo2o.api.customer.dto.response.AddressBookResDTO;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.customer.model.dto.request.AddressBookPageQueryReqDTO;
import com.jzo2o.customer.model.dto.request.AddressBookUpsertReqDTO;
import com.jzo2o.customer.model.dto.response.BooleanResDTO;
import com.jzo2o.customer.service.IAddressBookService;
import com.jzo2o.customer.service.ICommonUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.swing.*;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 普通用户相关接口
 *
 * @author itcast
 * @create 2023/7/7 19:34
 **/
@Validated
@RestController("AddressBookController")
@RequestMapping("/consumer/address-book")
@Api(tags = "用户端 - 地址簿相关接口")
public class AddressBookController {
    @Resource
    private IAddressBookService iaddressBookService;
    @PostMapping("")
    @ApiOperation("新增地址簿接口")
    public void addAddressBook(@RequestBody AddressBookUpsertReqDTO addressBookUpsertReqDTO) {
        iaddressBookService.addAddressBook(addressBookUpsertReqDTO);
    }


    @GetMapping("/page")
    @ApiOperation("地址薄分页查询")
    public PageResult<AddressBookResDTO> page(AddressBookPageQueryReqDTO
                                                          addressBookPageQueryReqDTO) {
        return iaddressBookService.pageAddressBook(addressBookPageQueryReqDTO);
    }


    @GetMapping("/{id}")
    @ApiOperation("地址薄详情接口")
        public AddressBookResDTO getAddressBookDetail(@NotNull(message = "id不能为空") @PathVariable("id") Long id)
    {

        return iaddressBookService.getAddressBookDetailById(id);

    }
    @PutMapping("/{id}")
    @ApiOperation("地址薄修改")
    public void updateAddressBook(@RequestBody
                                               AddressBookUpsertReqDTO addressBookUpsertReqDTO,
                                           @PathVariable("id") Long id)
    {
         iaddressBookService.updateAddressBook(addressBookUpsertReqDTO,id);
    }


    @DeleteMapping("/batch")
    @ApiOperation("地址簿批量删除接口")
//          todo 文档上只是说body 但不一定是json传参，需要测试的时候看一下是否能拿到

//    @RequestBody 通常用于接收 JSON 对象
//    前端传 ["1","2","3"] 时，
//    Spring 会尝试解析为 JSON 数组
//    但你的参数类型是 String，会导致解析失败
//    @NotNull(message="ids不能为空") @RequestBody String ids  是不可以的
    public void batchDeleteAddressBook(@NotNull(message="ids不能为空") @RequestBody List<Long> ids)
    {
        iaddressBookService.batchDeleteAddressBook(ids);
    }

    @PutMapping("/default")
    @ApiOperation("设置默认地址")
    public void setDefaultAddress(@RequestParam("flag") Long flag,
                                  @RequestParam("id") Long id)
    {
        iaddressBookService.setDefaultAddress(flag,id);
    }

    @GetMapping("/defaultAddress")
    @ApiOperation("获取默认地址")
    public AddressBookResDTO getDefaultAddress()
    {
        return iaddressBookService.getDefaultAddress();
    }


}
